#include "widget.h"
#include "./ui_widget.h"


Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    ui->label->installEventFilter(this);
}

Widget::~Widget()
{
    delete ui;
}

//步骤2：重写eventfilter事件
bool Widget::eventFilter(QObject *obj, QEvent *e)
{
    if(obj == ui->label) //判断控件
    {
        if(e->type() == QEvent::MouseButtonPress)
        {
            QMouseEvent *event = static_cast<QMouseEvent *>(e);
            QString str = QString("事件过滤器中鼠标按下; x = %1,y = %2").arg(event->x()).arg(event->y());
            qDebug() << str.toUtf8().data();

            return true;
        }
    }

    //只回时交给父类处理
    return QWidget::eventFilter(obj,e);
}
